<?php
  class admin extends Controller
  {
      protected $_templates;
	  
      function admin()
	  {
          parent::Controller();
          $this->pre_message = "";         
          $this->permit_library->check_permit();
          $this->session->set_userdata(array('Url'=>uri_string()));
          $this->load->model('admin_model','admin');
      }
      
      function listadmin()
	  {
          $data['title'] = lang('admin_title');
          $config['base_url'] = base_url().'admin/listadmin/';  
          $config['total_rows']   =  $this->admin->getNumAdmin();
          $data['num'] = $config['total_rows'];
          $config['per_page']  =   20;
          $config['uri_segment'] = 3; 
          $this->pagination->initialize($config);   
          $data['list'] =   $this->admin->getAllAdmin($config['per_page'],$this->uri->segment('3'));
          $data['pagination']    = $this->pagination->create_links();           
          $this->_templates['page'] = 'admin/listadmin';
          $this->site_library->load($this->_templates['page'],$data);
      }
      
      function add()
	  {                         
          $data['title'] = lang('admin_add');
          $data['list'] = $this->admin->get_list_permit();
          if(!empty($_POST['bt_submit']))
		  {
             $this->form_validation->set_rules('LoginName','Tên đăng nhập','trim|required|callback__check_admin');  
             $this->form_validation->set_rules('Passwd','Mật khẩu','trim|required');  
             $this->form_validation->set_rules('re_Passwd','Nhắc lại mật khẩu','trim|required|matches[Passwd]'); 
             if($this->form_validation->run() == FALSE)
			 {
                $this->pre_message = validation_errors();
             }
			 else
			 {
                 if($this->admin->saveAdmin())
				 {
                     $this->session->set_flashdata('message',lang('save_success'));
                     redirect('admin/listadmin');
                 }
				 else
				 {
                     $this->pre_message = lang('save_fail');
                 }
             } 
          }
		  
          $data['message'] = $this->pre_message;
          $this->_templates['page'] = 'admin/add';
          $this->site_library->load($this->_templates['page'],$data);
      }
	  
      function edit()
	  {
          $rs = $this->admin->getAdmin();
          $data['list'] = $this->admin->get_list_permit(); 
          $data['title'] = lang('admin_update').$rs->LoginName;
          $data['AdminID'] = $rs->AdminID;
		  
          if(!empty($_POST['bt_submit']))
		  {
             $this->form_validation->set_rules('LoginName','Tên đăng nhập','trim|required|callback__check_admin_edit');  
             $this->form_validation->set_rules('Passwd','Mật khẩu','trim');  
             $this->form_validation->set_rules('re_Passwd','Nhắc lại mật khẩu','trim|matches[Passwd]'); 
             if($this->form_validation->run() == FALSE)
			 {
                $this->pre_message = validation_errors();
             }
			 else
			 {
                 if($this->admin->saveAdmin())
				 {
                     $this->session->set_flashdata('message',lang('save_success'));
                     redirect('admin/listadmin');
                 }
				 else
				 {
                     $this->pre_message =lang('save_fail');
                 }
             }
             $data['LoginName'] = $this->input->post('LoginName');
          }
		  else
		  {
              $data['LoginName'] = $rs->LoginName;
          }
		  
          $data['message'] = $this->pre_message;
          $this->_templates['page'] = 'admin/edit';
          $this->site_library->load($this->_templates['page'],$data);
      }   
	     
      function _check_admin($LoginName)
	  {
          $this->db->where('LoginName',$LoginName);
          $query = $this->db->get('admin');
          if($query->num_rows() > 0)
		  {
              $this->form_validation->set_message('_check_admin', lang('check_admin'));
              return FALSE;
          }
		  else
		  {
              return TRUE;
          }
      }
      function _check_admin_edit($LoginName)
	  {
          $this->db->where('AdminID <>',$this->uri->segment(3));
          $this->db->where('LoginName',$LoginName);
          $query = $this->db->get('admin');
          if($query->num_rows() > 0)
		  {
              $this->form_validation->set_message('_check_admin_edit', lang('check_admin'));
              return FALSE;
          }
		  else
		  {
              return TRUE;
          }
      }      
      
      function del()
	  {
          $id = $this->uri->segment(3);
          $page = $this->uri->segment(4);
            if($this->admin->delete($id))
                $this->session->set_flashdata('message',lang('save_success'));
            else $this->session->set_flashdata('message',lang('save_fail'));
			
          redirect('admin/listadmin/'.$page);
      }
      
      function dels()
      {
            if(!empty($_POST['ar_id']))
            {
                $page = (int)$this->input->post('page');
                $ar_id = $this->input->post('ar_id');
                
                if(!empty($_POST['btn_submit']))
                {
                    for($i = 0; $i < sizeof($ar_id); $i ++)
					{
                        if ($ar_id[$i])
						{
                            if($this->admin->delete($ar_id[$i]))
                                $this->session->set_flashdata('message',lang('save_success'));
                            else $this->session->set_flashdata('message',lang('save_fail'));
                        }
                    }
                }
            }
			
            redirect('admin/listadmin/'.$page);
      }
   
  }
?>
